import axios from 'axios'
import { Message } from 'element-ui' //引入element的消息框，用于post修改请求时的消息提示，可选
//import store from '@/store/store.js' //引入vuex中的状态，引用名和路径根据项目实际情况，可选
var domain = ""; //api域名
var baseParams = { //基础参数
    token: "",
    userid: 0
        //......
};

function dataGet(apiName, params, callback) {
    var url = domain + apiName;
    var obj = initFn(params, callback, arguments[1]); //init方法实现见后
    var nparams = obj.nparams;
    callback = obj.callback;

    axios.get(url, { params: nparams }).then((response) => {
        var all = response.data; //返回所有数据
        var data = response.data.data; //根据后端实际返回修改

        //根据后端实际返回修改
        if (all.state == 1) { //成功
            if (callback) callback(data, all);
        } else { //失败
            console.log(all.state, all.message);
        }

    }).catch((error) => {
        console.log(error);
    });
}

function dataPost(apiName, params, callback) {
    var url = domain + apiName;
    var obj = initFn(params, callback, arguments[1]);
    var nparams = obj.nparams;
    callback = obj.callback;

    axios.post(url, nparams).then((response) => {
        var all = response.data;
        var data = response.data.data; //根据后端实际返回修改

        //根据后端实际返回修改
        if (all.state == 1) { //成功
            if (callback) callback(data, all);
        } else { //失败
            console.log(all.state, all.message);
        }

    }).catch((error) => {
        console.log(error);
    });
}

function dataPostXD(apiName, params, callback) {
    var url = domain + apiName;
    var obj = initFn(params, callback, arguments[1]);
    var nparams = obj.nparams;
    callback = obj.callback;

    axios.post(url, nparams).then((response) => {
        var all = response.data;
        var data = response.data.data; //根据后端实际返回修改

        //根据后端实际返回修改
        if (all.state == 1) { //成功
            Message({ message: '操作成功！', type: 'success' });
            if (callback) callback(data, all);

        } else { //失败
            console.log(all.state, all.message);
            Message({ message: '操作失败！', type: 'error' });
        }

    }).catch((error) => {
        console.log(error);
        Message({ message: '操作失败！', type: 'error' });
    });
}

function initFn(params, callback, argument) {
    //如果没有参数
    if (typeof argument == "function") {
        callback = argument;
        params = {};
    }
    //对象合并,确定最终参数
    return {
        nparams: $.extend({}, params, baseParams),
        callback: callback
    }

}
export {
    dataGet,
    dataPost,
    dataPostXD
}